package com.hrt.datasender.web.operInfo;

import com.btcode.common.MyLog;
import com.btcode.db.result.Row;
import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.log.ILog;
import com.hrt.datasender.datadao.model.BcSendPlatformParamModel;
import com.hrt.datasender.datadao.service.BcSendPlatformParamService;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.OperInfoModel;
import com.hrt.datasender.web.strategy.StrategyModel;
import com.hrt.datasender.web.strategy.StrategyService;
import com.hrt.db.DBManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public final class OperInfoService {

    private static volatile OperInfoService instance;

    private ILog log;
    /**
     * 运行信息日志
     */
    private ILog operInfolog;

    /**
     * 运行错误信息日志
     */
    private ILog operErrorInfolog;

    /**
     * 运行错误信息列表
     */
    public ArrayList<OperInfoModel> operErrorList;


    /**
     * 运行错误信息最大保留个数
     */
    private final int errorInfoMaxSize = 1000;

    private OperInfoService() {
        log = MyLog.getInstance().getLogger(getClass());
        operInfolog =  MyLog.getInstance().getLogger("operInfolog");
        operErrorInfolog = MyLog.getInstance().getLogger("operErrorInfolog");
        operErrorList = new ArrayList<OperInfoModel>();
    }

    public static OperInfoService getInstance() {
        if (instance == null) {
            synchronized (OperInfoService.class) {
                if (instance == null) {
                    instance = new OperInfoService();
                }
            }
        }
        return instance;
    }

    /**
     * 记录运行信息
     */
    public void info(Object message) {
        String value = message + "";
        operInfolog.info(value);
        OperInfoModel operInfoModel = new OperInfoModel(value);
        MqKit.productOperInfo(operInfoModel);
    }

    /**
     * 记录运行信息
     */
    public void info(Object message,String ip,int port) {
        String value = message + "";
        OperInfoModel operInfoModel = new OperInfoModel(value,ip,port+"");
        MqKit.productOperInfo(operInfoModel);
    }

    /**
     * 记录运行错误信息
     */
    public void error(Object message,Throwable throwable) {
        String value = message + "";
        operErrorInfolog.info(value);
        if(operErrorList.size()>errorInfoMaxSize){
            operErrorList.clear();
        }
        operErrorList.add(new OperInfoModel(value,throwable+""));
    }

    /**
     * 获取运行信息
     */
    public OperInfoModel getOperInfo(String server){
        while(true){
            OperInfoModel model = MqKit.getOperInfo();
            if(model==null){
                return model;
            }
            if(model.getIp()==null){
                return model;
            }
            String msgServer = model.getIp()+":"+model.getPort();
            if(msgServer.equals(server)){
                return model;
            }
        }
    }


    /**
     * 清空运行信息
     */
    public void clearOperInfo(){
        MqKit.clearOperInfo();
    }

    /**
     * 获取运行错误信息
     */
    public ArrayList<OperInfoModel> getOperErrorList(){
        return operErrorList;
    }

    /**
     * 获取运行错误信息个数
     */
    public int getOperErrorSize(){
        return operErrorList.size();
    }

    /**
     * 获取运行信息个数
     */
    public int getOperInfoSize(){
        String queueName = "operInfo";
        return MqKit.getMqMsgSize(queueName);
    }


    /**
     * 获取数据存储信息
     */
    public HashMap<String,String> getDataStorageInfo(String platformId)
            throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{
        HashMap<String,String> result = new HashMap<>();
        List<BcSendPlatformParamModel> list = BcSendPlatformParamService.getInstance().getPlatformParam(platformId);
        List<StrategyModel> strategys = StrategyService.getInstance().getListByPlatform(platformId);
        Calendar cal = Calendar.getInstance();
        int curSecond = cal.get(Calendar.HOUR_OF_DAY)*60*60 +cal.get(Calendar.MINUTE)*60+cal.get(Calendar.SECOND);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        for (int i = 0; i < strategys.size(); i++) {
            String tableName = strategys.get(i).getStrategyTable();
            String sql = "select count(datatime) as size from "+tableName+" where datatime > @datatime";
            SQLParameterList parameterList = new SQLParameterList();
            parameterList.put("datatime", cal.getTimeInMillis());
            List<Row> row = DBManager.getDataExecuter().queryList(sql,parameterList);
            int curDataSize = (int)row.get(0).getSafeDouble("size");
            int corDataSize = (curSecond - strategys.get(i).getDataInterval()) / strategys.get(i).getDataInterval();
            corDataSize = corDataSize * list.size();
            result.put(strategys.get(i).getStrategyId()+".curDataSize",curDataSize+"");
            result.put(strategys.get(i).getStrategyId()+".corDataSize",corDataSize+"");
        }
        return result;
    }

}
